package orgs.example.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import orgs.example.pojo.Cancellation;
import orgs.example.pojo.MeetingRoom;
import orgs.example.pojo.Reservation;

import java.util.List;

@Mapper
public interface CustomerMapper {

//  根据用户名查找订单
    @Select("select * from reservation where user_id=#{id}")
    List<Reservation> searchReservationByUserId(Integer id);

//   根据订单号改变订单状态
    @Update("update reservation set reserve_status=#{status} where reserve_id=#{reserveId}")
    void changeReservationStatus(Integer reserveId, String status);

//  根据订单号改变对应房间的状态
    @Update("update meeting_room set room_status=#{status} where room_id=#{roomId}")
    void changeRoomStatus(Integer roomId, String status);

//  根据房间号找房间
    @Select("select * from meeting_room where room_id=#{roomId}")
    MeetingRoom searchRoomByRoomId(Integer roomId);

//  添加退订订单
@Insert({
        "<script>",
        "INSERT INTO cancellation ",
        "<trim prefix='(' suffix=')' suffixOverrides=','>", // 使用 trim 标签处理列名列表
        "<if test='reserveId != null'>reserve_id,</if>",
        "<if test='cancelOrdertime != null'>cancel_ordertime,</if>",
        "<if test='cancelAmount != null'>cancel_amount,</if>",
        "<if test='cancelStatus != null'>cancel_status,</if>",
        "<if test='cancelProcessid != null'>cancel_processid,</if>",
        "<if test='cancelProcesstime != null'>cancel_processtime,</if>", // 最后一个也保留逗号，让 trim 去掉
        "</trim>",
        "VALUES ",
        "<trim prefix='(' suffix=')' suffixOverrides=','>", // 使用 trim 标签处理 VALUES 列表
        "<if test='reserveId != null'>#{reserveId},</if>",
        "<if test='cancelOrdertime != null'>#{cancelOrdertime},</if>",
        "<if test='cancelAmount != null'>#{cancelAmount},</if>",
        "<if test='cancelStatus != null'>#{cancelStatus},</if>",
        "<if test='cancelProcessid != null'>#{cancelProcessid},</if>",
        "<if test='cancelProcesstime != null'>#{cancelProcesstime},</if>", // 最后一个也保留逗号，让 trim 去掉
        "</trim>",
        "</script>"
})

    void addCancellation(Cancellation cancellation);

//  根据订单号找退订订单
    @Select("select * from cancellation where reserve_id=#{reserveId}")
    List<Cancellation> searchCancellationByReserveId(Integer reserveId);

//  找所有的订单
    @Select("select * from reservation")
    List<Reservation> searchAllReservation();

//    增加订单
    @Insert("insert into reservation values (null,#{userId},#{roomId},#{reserveStart},#{reserveEnd},#{reserveCost},#{reserveStatus},#{reserveOrdertime},#{reserveDeadline})")
    void addReservation(Reservation reservation);

//    根据房间号查找订单
    @Select("select * from reservation where room_id = #{roomId}")
    List<Reservation> searchAllReservationByRoomId(Integer roomId);
}
